package com.gzhryc.shared_device.oem.h5.mng.controllers.member;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberVerificationVoucher;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberVerificationVoucherEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberVerificationVoucherService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberVerificationVoucherSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public class MemberVerificationVoucherController {

    @Route(value = { "/member/verification_voucher/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", MemberVerificationVoucherEnum.State.values());
        return "/member/verification_voucher/list.html";
    }

    @Route(value = "/member/verification_voucher/list.data")
    public ListResult<MemberVerificationVoucher> listData(HttpServletRequest request) {
        ListResult<MemberVerificationVoucher> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        MemberVerificationVoucherSearch search = WebUtils.getParameter(request,MemberVerificationVoucherSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(MemberVerificationVoucherService.self().countBySearch(search));
        List<MemberVerificationVoucher> verificationVoucherList = MemberVerificationVoucherService.self().findBySearch(search,pageNo,pageLimit);
        if(verificationVoucherList != null && verificationVoucherList.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(verificationVoucherList);
        }
        return result;
    }

    @Route(value = {"/member/verification_voucher/add.html"})
    public String add(HttpServletRequest request) {
        return "/member/verification_voucher/edit.html";
    }

    @Route(value = { "/member/verification_voucher/add.do" })
    public AjaxResult doAdd(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        if (manager == null) {
            result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
            return result;
        }
        MemberVerificationVoucher verificationVoucher = WebUtils.getParameter(request,MemberVerificationVoucher.class);
        verificationVoucher.setSourceDescription(manager.getFullName() + "后台添加");
        try {
            if(MemberVerificationVoucherService.self().add(verificationVoucher)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/member/verification_voucher/info.html" })
    public String info(HttpServletRequest request) {
        String id = WebUtils.getString(request,"id");
        MemberVerificationVoucher verificationVoucher = MemberVerificationVoucherService.self().getById(id);
        WebUtils.setAttribute(request,verificationVoucher);
        return "/member/verification_voucher/info.html";
    }

    @Route(value = { "/member/verification_voucher/del.do" })
    public AjaxResult del(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String id = WebUtils.getString(request, "id");
        try {
            if(MemberVerificationVoucherService.self().deleteById(id)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

}
